﻿// HSS.Interlink.UI.StateManager.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.html)
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2012
// ----------------------------------------------------------------------------
// File:       StateManager.cs
// Author:     HSS\gbanta
// Created:    01/17/2012
// Modified:   01/17/2012
// ----------------------------------------------------------------------------
namespace HSS.Interlink.UI
{
	#region Using Directives
	using System;
	using System.Windows;
	using System.Windows.Controls;
	#endregion

	#region StateManager
	/// <summary>
	/// A Visual State Manager for MVVM.
	/// </summary>
	public class StateManager : DependencyObject
	{
		/// <summary>
		/// Gets the visual state.
		/// </summary>
		/// <param name="obj"></param>
		/// <returns></returns>
		public static string GetState(DependencyObject obj)
		{
			if (null == obj)
				throw new ArgumentNullException("obj");
			return (string)obj.GetValue(StateProperty);
		}
		/// <summary>
		/// Sets the visual state.
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="value"></param>
		public static void SetState(DependencyObject obj, string value)
		{
			if (null == obj)
				throw new ArgumentNullException("obj");
			obj.SetValue(StateProperty, value);
		}
		/// <summary>
		/// The Visual StateProperty Attached Property.
		/// </summary>
		public static readonly DependencyProperty StateProperty =
			DependencyProperty.RegisterAttached("State", typeof(string), typeof(StateManager), new PropertyMetadata((s, e) =>
				{
					var propertyName = (string)e.NewValue;
					var ctrl = s as Control;
					if (ctrl == null)
						throw new InvalidOperationException("This attached property only supports types derived from Control.");
					System.Windows.VisualStateManager.GoToState(ctrl, propertyName, true);
				}
			));
	}
	#endregion
}